using System;
using System.Collections.Generic;
using System.Text;
using SlimDX;
using Barrage.Global;

namespace Spacedash.Logic.Scenario.Cells
{
    /// <summary>
    /// Cell holds information of a cell in the scenario: Its color, when did the last ship passed by it, CellData instances over it, etc.
    /// </summary>
    public class Cell
    {
        // In miliseconds
        const int FADE_TIME = 30000;
        int row;
        int column;
        /// <summary>
        /// Cell's Row index in the scenario
        /// </summary>
        public int Row { get { return row; } }
        /// <summary>
        /// Cell's Column index in the scenario
        /// </summary>
        public int Column { get { return column; } }

        bool fixedColor = false;
        /// <summary>
        /// Gets / Sets wether the cell will change its color when ships pass over it or not.
        /// </summary>
        public bool FixedColor{get{return fixedColor;}set{fixedColor=value;}}
        bool active = false;
        /// <summary>
        /// Gets / Sets if the cell is active (that is, it can boost ships passing over it when following other ship's trail)
        /// </summary>
        public bool Active { get { return active; } set { active = true; } }
        
        CellData cellData = null;
        /// <summary>
        /// CellData instance positioned in this cell (can be null)
        /// </summary>
        public CellData CellData { get { return cellData; } set { cellData = value; } }

        Color3 baseColor = new Color3(1, 1, 1);
        /// <summary>
        /// Base color of the cell
        /// </summary>
        public Color3 BaseColor { get { return baseColor; } set { baseColor = value; } }
        
        Color3 shipColor = new Color3(1, 1, 1);

        Ship lastShip = null;
        /// <summary>
        /// Last Ship that passed over this cell
        /// </summary>
        public Ship LastShip { get { return lastShip; } }
        
        Color3 color = new Color3(1,1,1);
        /// <summary>
        /// Current color of this cell (varies depending of which ship passed by it)
        /// </summary>
        public Color3 Color { get { return color; } set { color = value; } }

        long initTime;

        /// <summary>
        /// Returns if the specified ship can obtain boost benefits for passing over this cell
        /// </summary>
        /// <param name="ship">Ship performing the check</param>
        /// <returns>True if the ship can be boosted</returns>
        public bool CanBoost(Ship ship)
        {
            return (active && lastShip != ship);
        }

        /// <summary>
        /// When this method is called, the cell will change its colour and data to allow other ships boosting benefits
        /// </summary>
        /// <param name="ship">Ship passing over this cell</param>
        public void ShipPassed(Ship ship)
        {
            lastShip = ship;
            this.shipColor = ship.Color;
            initTime = Timer.DefaultTimer.TotalTime;            
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="row">Row index</param>
        /// <param name="column">Column index</param>
        public Cell(int row, int column)
        {
            this.row = row;
            this.column = column;
        }

        /// <summary>
        /// Updates cell color based on time and passing ships.
        /// </summary>
        public void Update()
        {
            if (fixedColor)
            {
                active = false;
                return;
            }
            float progress = (Timer.DefaultTimer.TotalTime - initTime) / (float)FADE_TIME;
            if (progress<1)
                color = (Color3)Color4.Lerp((Color4)shipColor, (Color4)baseColor, progress);
            else
            {
                color = baseColor;
                active=false;
            }
        }
    }
}
